# -*- coding: UTF-8 -*-
# @Time : 2021-02-01 20:28
# @File : conftest.py
import time

import pytest
from common.Desired_Caps import appium_desired_caps
from common.StartAppium import get_devices, appium_start, check_port


# 1、定义命令行选项
def pytest_addoption(parser):
    parser.addoption('--cmdopt', action='store', default='run', help=None)


# 2、接收命令
@pytest.fixture(scope="session")
def cmdopt(request):
    return request.config.getoption("--cmdopt")


# 3、调用命令
@pytest.fixture(scope="session")
def atart_appium_desired(cmdopt):
        opt = eval(cmdopt)
        # cmdopt = {
        #     "host": "127.0.0.1",
        #     "port": "4723",
        #     "bpport": "4724",
        #     "uuid": None
        # }
        host = opt['host']
        port = opt['port']
        bpport = opt['bpport']
        uuid = opt['uuid']
        # systemPort = opt['systemPort']
        driver = None
        # if uuid in get_devices():
        #     appium_start(host, port, bpport, uuid)
        #     if not check_port():
        #         # driver = appium_desired_caps()
        #         driver = appium_desired_caps(host, port)  # , systemPort
        # 远程连接appium
        if not check_port(host, port):
            # driver = appium_desired_caps()
            driver = appium_desired_caps(host, port)
        # return driver
        yield driver
        driver.quit()
